function extremums = extremums(surface)
%EXTREMUMS calculate extremums for given 2D surface
surfaceSize = size(surface);
extremums = ones(surfaceSize) * mean(mean(surface));
for i = 1:surfaceSize(1) - 2
    for j = 1:surfaceSize(2) - 2
        window = surface(i:i + 2, j: j + 2);
        localExtremumMask = [1 1 1;
                             1 0 1;
                             1 1 1];
        if isequal(window < window(2, 2), localExtremumMask) || isequal(window > window(2, 2), localExtremumMask)
          extremums(i + 1, j + 1) = window(2, 2);
        end
    end
end
end

